Test Failed
Push — master ( 588283...272ac7 )
by Sergii
01:41
created

Parser.js ➔ ???   B

Complexity

Conditions 2
Paths 32

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 6
Metric Value
cc 2
c 6
b 0
f 6
nc 32
dl 0
loc 30
rs 8.8571
nop 2
1
const SecuritySchemaParser = require('./SecuritySchemesParser'),
2
      ParserInterface      = require('../ParserInterface'),
3
      spawn                = require('child_process'),
4
      _                    = require('lodash'),
5
      MethodsParser        = require('./MethodsParser')
6
7
/**
8
 * @ignore module.exports
9
 * @ignore exports
10
 * @ignore spawn
11
 */
12
13
/**
14
 * @class Parser
15
 * Parser for swagger
16
 *
17
 * @property {Swagger20} data Swagger data
18
 * @property {String|undefined} packageVersion Package version. If define command for get tags list not will be running
19
 * @property {String} getTagCommand Command for get tag lists. If <code>packageVersion</code> is empty
20
 * auto detect tag list and get last tag for detect package version
21
 * @property {String} packageName Package name
22
 * @property {String} className Class name
23
 * @property {String} moduleName Module name
24
 * @property {SecuritySchemaParser} schemeParser Security scheme parser
25
 * @property {MethodsParser} methodsParser Methods parser
26
 */
27
class Parser extends ParserInterface {
28
29
  /**
30
   * Constructor
31
   *
32
   * @param {Swagger20} data Swagger api data
33
   * @param {ParserOptions} options Parser options
34
   */
35
  constructor (data, options) {
36
    super()
37
38
    options = options || {}
39
40
    if (data.swagger !== '2.0') {
41
      throw new Error('Unsupported swagger version by parser')
42
    }
43
44
    this.data                   = data
45
    this.packageVersion         = options.packageVersion
46
    this.getTagCommand          = options.getTagCommand || 'git tag -l'
47
    this.repoPath               = options.repo || __dirname
48
    this.packageName            = options.packageName || 'test-api'
49
    this.schemaParser           = new SecuritySchemaParser(this.data.definitions)
50
    this.className              = options.className
51
    this.moduleName             = options.moduleName
52
    options.methodsParserConfig = options.methodsParserConfig || {
53
      parameterParserConfig: {
54
        addEnumDescription: true
55
      },
56
      className            : this.className,
57
      moduleName           : this.moduleName,
58
      packageName          : this.packageName
59
    }
60
    this.methodsParser          = new MethodsParser(this.data.paths, this.schemaParser, options.methodsParserConfig)
61
62
    this._initData(options)
63
    this._prepareDefinitions()
64
  }
65
66
  /**
67
   * Get package version
68
   *
69
   * @return {null|string}
70
   * @private
71
   */
72
  _getPackageVersion () {
73
    if (this.packageVersion) {
74
      return this.packageVersion
75
    }
76
77
    let gitTags = spawn.execSync('cd ' + this.repoPath + '; ' + this.getTagCommand).toString().split('\n').reverse()
78
    gitTags     = gitTags.slice(1, gitTags.length)
79
80
    if (!gitTags.length) {
81
      gitTags.push('dev-master@dev')
82
    }
83
84
    this.packageVersion = gitTags[0]
85
86
    return this.packageVersion
87
  }
88
89
  /**
90
   * Init base swagger data
91
   *
92
   * @param {ParserOptions} options Parser options
93
   * @private
94
   */
95
  _initData (options) {
96
    this.swagger                = {}
97
    this.swagger.title          = this.data.title
98
    this.swagger.description    = this.data.info.description
99
    this.swagger.apiVersion     = this.data.info.version
100
    this.swagger.packageVersion = this._getPackageVersion()
101
    this.swagger.isSecure       = typeof this.data.securityDefinitions !== 'undefined'
102
    this.swagger.className      = options.className
103
    this.swagger.moduleName     = options.moduleName
104
  }
105
106
  /**
107
   * Prepare definitions (project models)
108
   *
109
   * @private
110
   */
111
  _prepareDefinitions () {
112
    this.swagger.definitions      = this.data.definitions
113
    this.swagger.definitionsGroup = []
114
115
    let _self = this
116
117
    _.each(this.swagger.definitions, (definition, name) => {
118
      _self._addDefinitionToGroup(name, definition)
119
    })
120
  }
121
122
  /**
123
   * Generate definitions group
124
   *
125
   * @param {String} group Group name
126
   * @param {Object} definition Swagger definition
127
   * @private
128
   */
129
  _addDefinitionToGroup (group, definition) {
130
    if (typeof definition !== 'object') {
131
      return
132
    }
133
134
    if (typeof this.swagger.definitionsGroup[group] === 'undefined') {
135
      this.swagger.definitionsGroup[group] = []
136
    }
137
138
    this.swagger.definitionsGroup[group].push(definition)
139
  }
140
141
  /**
142
   * Parse data and return prepared data for templates
143
   *
144
   * @return {Swagger20}
145
   */
146
  parse () {
147
    this.swagger.security  = this.schemaParser.parse()
148
    this.swagger.methods   = this.methodsParser.parse()
149
    this.swagger.tagsGroup = this.methodsParser.methodsGroup
150
151
    return this.swagger
152
  }
153
}
154
155
module.exports = Parser